/////precompiled////////
#include "precompiled.h"
////////////////////////


#include "frm_control_TPanelWin.h"
using namespace frm;
using namespace control;

// TPanelWin //////////////////////////////////////////////////////////////////

//
TPanelWin::TPanelWin(control::TWindow &Window) : control::TPanel(Window) 
{
/********
  Filler.Set(filler::TArray()
   .Add(filler::TColor(255,255,255))
   .Add(filler::TLineVertCenter(192,192,192))
   .Add(filler::TLineHorzCenter(192,192,192))
  );
  
  Cursor.Set(cursor::TCursorWin(IDC_CROSS));
***********/

  grf::TWindow::SetParent(Window);
  GetGeometry().SetWidth (new geometry::TFixed(100));
  GetGeometry().SetHeight(new geometry::TFixed(100));
}

//
void TPanelWin::TComponent_Paint(TPaint &Paint,int NPaintStack)
{
  grf::TWindow::SetWindowRect(Paint.GRect.RThisVisible,true);
  grf::TRect R; R = Paint.GRect.RThis.Size();
  TPanelWin_SetWindowRect(R);
  grf::TWindow::Show();
}

//
void TPanelWin::TWindow_Paint(grf::TPaint &grfPaint)
{
  grf::TRect ClientRect = GetClientRect();
  TPaint     Paint;

  Paint.State        = TPaint::EState_PaintAll;
  Paint.DC           = grfPaint.DC;
  Paint.RClientPaint = grfPaint.DC.RClientPaint;

  Paint.GRect.RParent = ClientRect;
  Paint.GRect.RParentVisible = ClientRect;

  Paint.GRect.RThis = ClientRect;
  Paint.GRect.RThisVisible = ClientRect;

  if (GetStyle().Filler.IsTransparent()) {
    //TRect::TComponent_Paint(Paint,NPaintStack);
  }
  GetStyle().Filler.Paint(*this,Paint);
}

//
void TPanelWin::TWindow_Mouse(grf::TMouse &grfMouse) 
{
/****
  TMouse Param;
    Param.State    = grfMouse.State;
    Param.Shift    = grfMouse.Shift;
    Param.PtClient = grfMouse.PtClient;
*********/
  //TPanel::TComponent_Mouse(Param);
  TPanel::TComponent_SetCursor();
  //GetStyle().Cursor.Paint();
}


void TPanelWin::SetChildControl(HWND _hwnd){
	ChildWindow = _hwnd;
	oldChildProc =  (::WNDPROC) ::SetWindowLong( ChildWindow, GWL_WNDPROC, (LONG)ChildWndProc );
	::SetWindowLong( ChildWindow, GWL_USERDATA, (LONG)this );	
}

WNDPROC TPanelWin::oldChildProc = NULL;
LRESULT CALLBACK TPanelWin::ChildWndProc(HWND _hwnd, UINT _msg, WPARAM _wParam, LPARAM _lParam){
	TPanelWin *control = (TPanelWin*)::GetWindowLong( _hwnd, GWL_USERDATA );

    if (control) {
	    static const int SHIFTED = 0x8000;

		switch (_msg){
		    case WM_KILLFOCUS:{
			    if( _wParam == NULL )
					::SetFocus( control->GetWindow() );
			    //::MessageBeep(0);
			    break;
		    }
			case WM_KEYDOWN:{
		
				grf::TKeyboardKey Param;
				Param.State = Param.EState_Down;

				if (GetKeyState(VK_SHIFT) & SHIFTED){ 
					Param.Shift << grf::TShift::Shift;
				}
				if (GetKeyState(VK_CONTROL) & SHIFTED) 
					Param.Shift << grf::TShift::Ctrl;
				if (GetKeyState(VK_MENU) & SHIFTED) 
					Param.Shift << grf::TShift::Alt;

					Param.Key = _wParam;
			
					control->TComponent_KeyboardKey( Param );
				break;
			}
		}
/****************
	  ::SetWindowLong( _hwnd, GWL_USERDATA, control->oldUserData);	
    LRESULT Ret = ::CallWindowProc( oldChildProc, _hwnd, _msg, _wParam, _lParam );
	  ::SetWindowLong( _hwnd, GWL_USERDATA, (LONG)control );	
    return Ret;
***************/    
  }
  return ::CallWindowProc( oldChildProc, _hwnd, _msg, _wParam, _lParam );
}
